﻿using HslCommunication.Core;
using HslCommunication.ModBus;
using SuperX.Common.Log;
using SuperX.DataService.Abstract;
using SuperX.Helper.Util;
using SuperX.ModbusDriver;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Linq;

namespace SuperX.Simulator
{
    [Description("模拟驱动")]
    public class SimulatorDriver : ModbusBase
    {
        private ModbusTcpServer modbusTcpServer = new ModbusTcpServer();

        public override IByteTransform ByteTransform { get => modbusTcpServer.ByteTransform; }

        public override IReadWriteNet ReadWriteNet => modbusTcpServer;

        public override int Port { get; set; } = 560;

        private IDataServer _parent;
        private EasyThread _accumulateLoop;
        private short intLoop = 0;
        private bool boolLoop = false;

        public int UpdateInterval { get; set; } = 1000;

        /// <summary>
        /// 是否关闭
        /// </summary>
        public override bool IsClosed
        {
            get
            {
                return PlcReadWrite == null || !_connected;
            }
        }

        public SimulatorDriver()
        {
        }

        public SimulatorDriver(IDataServer server, string name, string nodeName) : base(server, name, nodeName)
        {
        }

        public override bool Connect()
        {
            modbusTcpServer.Port = Port;
            try
            {
                modbusTcpServer.ServerStart();
                modbusTcpServer.DataFormat = DataFormat;
                modbusTcpServer.Station = SalveId;
                PlcReadWrite = modbusTcpServer;
                _accumulateLoop = new EasyThread(Loop, UpdateInterval);
                _accumulateLoop.Start();
                _connected = true;
            }
            catch (Exception ex)
            {
                Logger.Warn($"{Name}连接失败,错误信息：{ex.Message}");
            }
            return _connected;
        }

        public override void Dispose()
        {
            foreach (var group in DriverGroup)
            {
                group.Dispose();
            }
            _accumulateLoop.Stop();
            DriverGroup?.Clear();
            modbusTcpServer?.ServerClose();
            PlcReadWrite = modbusTcpServer;
            modbusTcpServer = null;
            base.Dispose();
        }

        private void Loop()
        {
            intLoop++;
            boolLoop = !boolLoop;
            if (intLoop > 10000) intLoop = 0;
            short[] shortArray = new short[5000];
            bool[] boolArray = new bool[5000];
            for (int i = 0; i < 5000; i++)
            {
                shortArray[i] = intLoop;
                boolArray[i] = boolLoop;
            }
            var res = PlcReadWrite.Write("x=3;5000", shortArray);
            var res1 = PlcReadWrite.Write("x=1;5001", boolArray);
        }

        public override Dictionary<string, string> GetRegester()
        {
            return new Dictionary<string, string>()
            {

                { "x=1;","x=1;"},{ "x=2;","x=2;"},{ "x=3;","x=3;"},{ "x=4;","x=4;"}
            };
        }
    }
}
